﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using EQEmu.Database;

namespace EQEmu.Doors
{
    public class ZoneDoorsDatabase : ZoneDoors, IDisposable
    {
        private EQEmuContext _context = null;

        public ZoneDoorsDatabase(string zone, EQEmuContextFactory factory, int zoneVersion = 0)
            : base(zone,zoneVersion)
        {
            _context = factory.Create();
            var query = _context.doors.Where(x => zone == x.zone && zoneVersion == x.version);
            foreach (var door in query)
            {
                base.AddDoor(door);
            }
        }

        public override void AddDoor(doors door)
        {
            base.AddDoor(door);
            _context.doors.AddObject(door);
        }

        public override void RemoveDoor(doors door)
        {
            base.RemoveDoor(door);
            _context.doors.DeleteObject(door);
        }

        public override doors CreateDoor()
        {
            var door = _context.doors.CreateObject();
            door.zone = ZoneShortName;
            door.size = 100;
            return door;
        }

        public void CommitChanges()
        {
            if (_context != null)
            {
                _context.SaveChanges();
            }
        }

        public override bool HasModifications
        {
            get
            {
                if (_context != null)
                {
                    return _context.ObjectStateManager
                        .GetObjectStateEntries(
                            System.Data.EntityState.Added | 
                            System.Data.EntityState.Deleted | 
                            System.Data.EntityState.Modified).Any();
                }
                else return false;
            }
        }

        //public static ZoneDoorsDatabase LoadFromFile(string path, EQEmuContextFactory factory)
        //{
        //    ZoneDoorsDatabase zp = null;
        //    string zone = "";
        //    int version = GetVersionFromFileName(path);
        //    var spawns = (IEnumerable<spawn2>)(SerializationExtensions.FromXmlFile<spawn2[]>(path));
        //    if (spawns.Count() > 0)
        //    {
        //        zone = spawns.ElementAt(0).zone;
        //    }

        //    zp = new ZoneSpawnsDatabase(zone, factory, version);
        //    foreach (var sp in spawns.ToArray())
        //    {
        //        var exists = zp.Spawns.FirstOrDefault(x => x.id == sp.id);
        //        if (exists != null)
        //        {
        //            exists.CopyFrom(sp);
        //        }
        //        else
        //        {
        //            zp.AddSpawn(sp);
        //        }
        //    }

        //    return zp;
        //}

        protected virtual void Dispose(bool disposeAll)
        {
            if (_context != null)
            {
                _context.Dispose();
            }
        }

        public void Dispose()
        {
            Dispose(true);
        }
    }
}
